package com.system.manager.modules.projectLibrary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.system.manager.common.exception.SysException;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.entity.User;
import com.system.manager.modules.authorized.mapper.UserMapper;
import com.system.manager.modules.projectLibrary.entity.Project;
import com.system.manager.modules.projectLibrary.entity.ProjectChange;
import com.system.manager.modules.projectLibrary.enums.ProjectLibraryErrorCode;
import com.system.manager.modules.projectLibrary.exception.ProjectLibraryException;
import com.system.manager.modules.projectLibrary.mapper.ProjectChangeMapper;
import com.system.manager.modules.projectLibrary.mapper.ProjectMapper;
import com.system.manager.modules.projectLibrary.service.IProjectChangeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jisf
 * @since 2021-11-10
 */
@Service
public class ProjectChangeServiceImpl extends ServiceImpl<ProjectChangeMapper, ProjectChange> implements IProjectChangeService {

    @Autowired
    private ProjectChangeMapper projectChangeMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ProjectMapper projectMapper;


    public static Map<Integer, Project> projectChangeMap = new HashMap<>();

    /**
     * TODO 插入项目变更
     *
     * @param projectChange
     * @param project
     */
    @Override
    public void insertProjectChange(ProjectChange projectChange, Project project) {

        try {
            //插入项目变更
            projectChangeMapper.insert(projectChange);
        } catch (Exception e) {
            throw new SysException();
        }

        //记录项目变更
        Integer id = projectChange.getId();
        projectChangeMap.put(id, project);

    }

    /**
     * TODO 审核项目变更
     *
     * @param id
     */
    @Override
    public void updateProjectChange(Integer id) {

        //审核项目变更
        try {
            UpdateWrapper<ProjectChange> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("ratify", 1).eq("id", id);
            projectChangeMapper.update(new ProjectChange(), updateWrapper);
        } catch (Exception e) {
            throw new SysException();
        }

        //变更项目
        //修改项目信息
        try {
            projectMapper.updateById(projectChangeMap.get(id));
        } catch (Exception e) {
            throw new ProjectLibraryException(ProjectLibraryErrorCode.UPDATE_PROJECT_FAILURE);
        }

    }

    /**
     * TODO 查询项目变更
     *
     * @param subject
     * @return
     */
    @Override
    public List<ProjectChange> selectProjectChange(Subject subject) {
        Integer firm = userMapper.selectById(subject.getId()).getOrganizationId();
        QueryWrapper<ProjectChange> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("firm",firm);
        List<ProjectChange> projectChanges = projectChangeMapper.selectList(queryWrapper);
        return projectChanges;
    }

    /**
     * TODO 删除变更记录
     *
     * @param day
     * @param subject
     */
    public void deleteProjectChange(Integer day,Subject subject){
        Integer firm = userMapper.selectById(subject.getId()).getOrganizationId();
        long time = new Date().getTime()-(day*24*60*60*1000);
        Date date = new Date(time);

        QueryWrapper<ProjectChange> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("firm",firm);
        queryWrapper.lt("time",date);
        try {
            projectChangeMapper.delete(queryWrapper);
        }catch (Exception e){
            throw new SysException();
        }


    }
}
